<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="lib/d3.v7.min.js"></script>
    <style>
        .grid-container {
            width: 300px;
            height: 300px;
            position: relative;
        }

        .grid-item {
            width: 100px;
            height: 100px;
            border: 1px solid black;
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 24px;
            position: absolute;
        }

        .expanded {
            width: 100%;
            height: 100%;
            left: 0;
            top: 0;
            background-color: #f0f0f0;
            z-index: 10;
        }
    </style>
</head>
<body>
<div class="grid-container" id="grid"></div>
<script>
    // 定义九宫格的尺寸和间距
    const gridSize = 3;
    const size = 100;

    const grid = d3.select('#grid');

    // 创建九宫格
    const items = grid.selectAll('.grid-item')
        .data(d3.range(gridSize * gridSize))
        .enter()
        .append('div')
        .attr('class', 'grid-item')
        .style('left', (d) => (d % gridSize) * size + 'px')
        .style('top', (d) => Math.floor(d / gridSize) * size + 'px')
        .text((d) => d + 1)
        .on('click', function (event, d) {
            // 切换格子展开/折叠状态
            const expanded = d3.select(this).classed('expanded');
            items.classed('expanded', false); // 关闭所有已展开的格子
            if (!expanded) {
                d3.select(this).classed('expanded', true); // 展开当前格子
            }
        });

    // 展开格子的自定义内容
    const expandedContent = grid.append('div')
        .attr('class', 'expanded-content')
        .style('display', 'none')
        .style('width', '100%')
        .style('height', '100%')
        .style('position', 'absolute')
        .style('top', '0')
        .style('left', '0')
        .style('z-index', '5')
        .style('background-color', 'rgba(255,255,255,0.8)')
        .style('display', 'flex')
        .style('justify-content', 'center')
        .style('align-items', 'center')
        .style('font-size', '24px')
        .text('自定义内容');

    // 监听展开事件，显示自定义内容
    items.on('click', function (event, d) {
        if (d3.select(this).classed('expanded')) {
            expandedContent.style('display', 'flex');
        } else {
            expandedContent.style('display', 'none');
        }
    });

    // 为每个格子添加事件监听器（可选）
    items.on('click', function (event, d) {
        console.log('点击了格子', d + 1);
    });
</script>
</body>
</html>